/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GEOFENCE_POLICY_H
#define GEOFENCE_POLICY_H

#include <set>
#include <map>
#include <unistd.h>
#include "sensor_if.h"
#include "datetime_ex.h"
#include "geofence_data_utils.h"
#include "igeofence_algorithm_callback.h"
#include "igeofence_algorithm.h"
#include "igeofence_callback.h"
#include "geofence_common.h"
#include "geofence_location.h"
#include "geofence_manager.h"
#include "geofence_statemachine.h"
#include "geofence_track.h"
#include "geofence_config.h"
#include "geofence_impl_client.h"

namespace OHOS {
namespace Msdp {
class GeofenceService;
class GeofencePolicy {
public:
    explicit GeofencePolicy(const wptr<GeofenceService>& ms) : ms_(ms)
    {
        GEOFENCE_HILOGI(GEOFENCE_MODULE_SERVICE, "GeofencePolicy instance is created.");
    }
    ~GeofencePolicy() = default;

    class GeofenceCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
    public:
        GeofenceCallbackDeathRecipient() = default;
        virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
        virtual ~GeofenceCallbackDeathRecipient() = default;
    };

    bool Init();
    bool InitInterface();
    void GetLocationData(GeofenceDataUtils::LocationData& locationData);
    int32_t GetTimeNow();
    int32_t GetTimeNowDay();
    int32_t FoolproofTacticsTime();
    int32_t FoolproofTacticsDistance(double distance);
    int32_t GeofenceStateJudgement(std::vector<double> locationData);
    int32_t GeofenceEnterDistanceJudgement(std::vector<double> locationData);
    int32_t GeofenceExitDistanceJudgement(std::vector<double> locationData);
    int32_t DistanceFrequency(std::vector<double> locationData);
    void GeofenceState(std::vector<double> locationData);
    void JudgeGeofenceStateNotify(GeofenceDataUtils::GeofenceData &geofencecallbackData, int32_t stateFlag);
    void NotifyGeofenceChange(const GeofenceDataUtils::GeofenceData& geofenceData);
    void SubscribePoint(const GeofenceDataUtils::GeofenceType& type, const GeofenceDataUtils::PointOptions& pointOptions,
        const sptr<IgeofenceCallback>& callback);
    void UnSubscribePoint(const GeofenceDataUtils::GeofenceType& type, const GeofenceDataUtils::PointOptions& pointOptions,
        const sptr<IgeofenceCallback>& callback);
    void GetCloudData(GeofenceDataUtils::CloudData& cloudData);
    bool CloudTileStrategy(const GeofenceDataUtils::PoiOptions& poiOptions);
    void SubscribePoi(const GeofenceDataUtils::GeofenceType& type, const GeofenceDataUtils::PoiOptions& poiOptions,
        const sptr<IgeofenceCallback>& callback);
    void UnSubscribePoi(const GeofenceDataUtils::GeofenceType& type, const GeofenceDataUtils::PoiOptions& poiOptions,
        const sptr<IgeofenceCallback>& callback);
    bool SetFoolproofTactics(const GeofenceDataUtils::GeofenceType& type, double minute);
    int32_t GetFoolproofTacticstime();
    int32_t GetStateCache();

private:
    struct classcomp {
        bool operator()(const sptr<IgeofenceCallback> &l, const sptr<IgeofenceCallback> &r) const
        {
            return l->AsObject() < r->AsObject();
        }
    };
    std::mutex mutex_;
    std::map<GeofenceDataUtils::GeofenceType, std::set<const sptr<IgeofenceCallback>, classcomp>> listenerMap_;
    const wptr<GeofenceService> ms_;
    sptr<IRemoteObject::DeathRecipient> geofenceCBDeathRecipient_;

    std::shared_ptr<GeofenceImplClient> geofenceImplClient_ = std::make_shared<GeofenceImplClient>();
    std::shared_ptr<GeofenceLocation> geofenceLocation_ = std::make_shared<GeofenceLocation>();
    std::shared_ptr<GeofenceManager> geofenceManager_ = std::make_shared<GeofenceManager>();
    std::shared_ptr<GeofenceConfig> geofenceConfig_ = std::make_shared<GeofenceConfig>();
    std::unique_ptr<GeofenceTrack> geofenceTrack_;
    std::unique_ptr<GeofenceStateMachine> geofenceStateMachine_;
    int32_t stateFlag_ = -1;
    int32_t stateCache_ = -1;
    int32_t judgementTimeFlag_ = 0;
    int32_t lastJudgementTimeFlag_ = 0;
    int32_t lastNotifyTimeFlag_ = 0;

    double timeMinuteEnterAndExit_;
    double timeMinuteLessOneKm_;
    double timeMinuteOneToTenKm_;
    double timeMinuteTenToThirtyKm_;
    double timeMinuteMoreThirtyKm_;
    int32_t repeatEntryTimes_;
    double timesEnterRadius_;
    double timesExitRadius_;
    int32_t entryTimes_ = 0;
    int32_t timeDay_;

    GeofenceDataUtils::GeofenceType type_ = GeofenceDataUtils::GeofenceType::GEOFENCE_OPTION_DEFAULT;
    GeofenceDataUtils::PointOptions pointOptions_;
    GeofenceDataUtils::CloudData cloudData_;
    int32_t foolTacticsTime_ = 0;

};
} // namespace Msdp
} // namespace OHOS
#endif // GEOFENCE_POLICY_H
